package com.liy.framework.common.utils;

import java.util.*;
import java.util.regex.*;
import java.lang.reflect.*;

public class Tools
{
    private static Pattern linePattern;
    private static Pattern humpPattern;
    
    public static void main(final String[] args) {
    }
    
    public static String getNewUUID() {
        final String str = UUID.randomUUID().toString().replace("-", "");
        return str;
    }
    
    public static String getNewUUIDHashCode() {
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        if (hashCodeV < 0) {
            hashCodeV = -hashCodeV;
        }
        return String.format("%010d", hashCodeV);
    }
    
    public static String getOrderID18() {
        final String date = DateUtil.getTimeNow("yyyyMMdd");
        return date + getNewUUIDHashCode();
    }
    
    public static String getStringValue(final Object obj) {
        if (obj == null) {
            return "";
        }
        return obj.toString();
    }
    
    public static boolean StringIsNullOrSpace(final String paramString) {
        return paramString == null || "".equals(paramString.trim()) || "NULL".equals(paramString.trim().toUpperCase()) || "undefined".equals(paramString.trim().toLowerCase());
    }
    
    public static boolean ObjectIsNullOrSpace(final Object obj) {
        return obj == null || "".equals(obj.toString()) || "NULL".equals(obj.toString().toUpperCase());
    }
    
    public static boolean listIsNullOrEmpty(final List list) {
        return list == null || list.size() <= 0;
    }
    
    public static boolean arrayIsNullOrEmpty(final Object[] array) {
        return array == null || array.length <= 0;
    }
    
    public static String lineToHump(String str) {
        str = str.toLowerCase();
        final Matcher matcher = Tools.linePattern.matcher(str);
        final StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
    
    public static String humpToLine2(final String str) {
        final Matcher matcher = Tools.humpPattern.matcher(str);
        final StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
    
    public static String toLowerCaseFirstOne(final String s) {
        if (Character.isLowerCase(s.charAt(0))) {
            return s;
        }
        return Character.toLowerCase(s.charAt(0)) + s.substring(1);
    }
    
    public static String toUpperCaseFirstOne(final String s) {
        if (Character.isUpperCase(s.charAt(0))) {
            return s;
        }
        return Character.toUpperCase(s.charAt(0)) + s.substring(1);
    }
    
    public static Object getClassValueByKey(final Object obj, final String key) {
        final Class userCla = obj.getClass();
        final Field[] fs = userCla.getDeclaredFields();
        for (int i = 0; i < fs.length; ++i) {
            final Field f = fs[i];
            f.setAccessible(true);
            try {
                if (f.getName().endsWith(key)) {
                    return f.get(obj);
                }
            }
            catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
            catch (IllegalAccessException e2) {
                e2.printStackTrace();
            }
        }
        return "";
    }
    
    public static void setClassValueByKey(final Object obj, final String key, final Object value) {
        try {
            final Class<?> clazz = obj.getClass();
            final Field field = clazz.getDeclaredField(key);
            if (field != null) {
                field.setAccessible(true);
                field.set(obj, value);
            }
        }
        catch (Exception ex) {}
    }
    
    public static String strSubstring(final String str, final int start, final int length) {
        return str.substring(start, start + length);
    }
    
    static {
        Tools.linePattern = Pattern.compile("_(\\w)");
        Tools.humpPattern = Pattern.compile("[A-Z]");
    }
}
